class Response:
    def __init__(self, conn):
        self.conn = conn
        self.protocol = "HTTP/1.1"
        self.code = "200"
        self.msg = "OK"
        self.header = {"Content-Type": "text/html;charset=utf-8"}
        self.data = ""

    def buildResponse(self):
        res = ""
        res += self.protocol + " " + self.code + " " + self.msg + "\r\n"
        for key in self.header:
            res += key + ": " + self.header[key] + "\r\n"
        res += "\r\n" + self.data
        return res

    def set_ContentType(self, value):
        self.header["Content-Type"] = value

    def set_Field(self, key, value):
        self.header.update({str(key): str(value)})

    def write(self, data):
        self.data += str(data)
        return self

    def setData(self):
        self.header.update({"Content-Length": str(len(self.data.encode()))})

    def submit(self):
        self.setData()
        self.conn.send(self.buildResponse().encode())
        self.conn.close()

    def to_String(self):
        return self.buildResponse()


if __name__ == '__main__':
    response = Response("a")
    response.write("<h1>Hello World!!!</h1>")
    print(response.to_String())
